<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/2/6
 * Time: 19:14
 */

namespace Home\Controller;
use Org\Util\Snoopy;
use Think\Controller;

class KbController extends Controller{
    public function _initialize(){
        header("content-type:text/html; charset=utf-8");
        $flag = session("?Account");
        if(!$flag){
            $this->error("非法登录",U('Home/Index/index'));
        }
    }

    public function index(){


        $sn=new Snoopy();
        $Account=session("Account");
        $sn->cookies= session("cookies");
        $sn->proxy_host=session("ip.ip");
        $sn->proxy_port=session("ip.port");
        $sn->agent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
        $sn->referer = "http://jw.gxsdxy.cn/jiaowures/";
        $sn->rawheaders['Pragma'] = 'no-cache';
        $sn->rawheaders['X_FORWARDED_FOR'] = "jw.gxsdxy.cn";
        $sn->fetch("http://jw.gxsdxy.cn/jiaowures/JWXS/pkgl/XsKB_List.aspx?usermain=x{$Account}");

        $ret=mb_convert_encoding($sn->results,"utf-8","gb2312");
        $pattern = '/<option( selected="selected")? value="(.{4,})">/U';
        preg_match_all($pattern, $ret, $matches);
        $xn=$matches[2];

        $pattern = '/<option( selected="selected")? value="(.{1,2})">/U';
        preg_match_all($pattern, $ret, $matches);
        $zs=$matches[2];

        $pattern = '/<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" \/>/is';
        preg_match_all($pattern, $ret, $matches1);
        $VIEWSTATE=$matches1[1][0];
//        dump($matches1);


        if(IS_POST){
            $sn2=new Snoopy();
            $url="http://jw.gxsdxy.cn/jiaowures/JWXS/pkgl/XsKB_List.aspx?usermain=x{$Account}";
            $sn2->cookies= session("cookies");
            $sn2->proxy_host=session("ip.ip");
            $sn2->proxy_port=session("ip.port");
            $sn2->agent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
            $sn2->referer = "http://jw.gxsdxy.cn/jiaowures/";
            $sn2->rawheaders['Pragma'] = 'no-cache';
            $sn2->rawheaders['X_FORWARDED_FOR'] = "jw.gxsdxy.cn";
//            $sn2->fetch("http://jw.gxsdxy.cn/jiaowures/JWXS/pkgl/XsKB_List.aspx?usermain=x{$Account}");

            $post["__EVENTTARGET"]="ddlxnxqh";
            $post["__EVENTARGUMENT"]="";
            $post["__LASTFOCUS"]="";
            $post["__VIEWSTATE"]=$VIEWSTATE;
            $post["ddlxnxqh"]=I("post.xn");
            $post["mx"]="on";
            $post["zc"]=I("post.zs");
            if(!empty($post["zc"])){
                $post["__EVENTTARGET"]="zc";
            }
//            dump($post);
            $sn2->submit($url,$post);

//            $sn2->results;
//            $cxkb=htmlentities($sn2->results);

            $ret=mb_convert_encoding($sn2->results,"utf-8","gb2312");
//            echo htmlentities($ret);
            $pattern = '/ <table border=1 width=98% height=80% bordercolorlight=#588FC7 cellspacing=0 cellpadding=0 bordercolor=#588FC7 bordercolordark=#ffffff height=88>(.*?)<\/table>/is';
            preg_match_all($pattern, $ret, $matches);
//            dump($matches);
            $cxkb=$matches[1][0];
            if(!empty($cxkb)){
                $this->assign("cxkb",$cxkb)->assign("xzxn",$post["ddlxnxqh"])->assign("xzzs",$post["zc"]);
            }else{
                $pattern = '/ <span id="Label1" style="color:Red;">(.*?)<\/span>/is';
                preg_match_all($pattern, $ret, $matches);
//            dump($matches);
                $cxkb=$matches[1][0];
                $this->assign("cxkb","<div style='color: red'>".$cxkb."</div>")->assign("xzxn",$post["ddlxnxqh"]);
            }




        }


        $this->assign("xn",$xn)->assign("zs",$zs);
        $this->assign("title","查询课表");
        $this->display();
    }
}